<?php
namespace Destroyer\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Str;

use App\Http\Controllers\Controller;
use Destroyer\Models\User;
use Destroyer\Models\Menu;
use Destroyer\Admin;


class NavController extends Controller
{

	//
	public function __invoke(Request $request)
	{
		$menuModels = Menu::where('type', 'M')->orderBy('sort')->get();

		$menuModels = $menuModels->map(function($menu){
			$data = [
				'id'          => $menu->id,
				'parent_id'   => $menu->parent_id,

				'title'       => $menu->title ,
				// 'name'        => empty($menu->name) ? Str::camel(str_replace('/', '_', $menu->component)) : $menu->name ,
				'name'        => Str::camel(str_replace('/', '_', ($menu->component??$menu->path))),
				'path'        => empty($menu->path) ? '/'. $menu->component : $menu->path ,
				'component'   => $menu->component,
				'icon'        => $menu->icon ? $menu->icon : '',//$menu->icon
			];

			return $data;
		});

		$data = buildTree($menuModels, null, 'id', 'parent_id', function($item){
			unset($item['parent_id']);
			return $item;
		});

		return response()->admin($data);
	}

}
